<?php

namespace app\admin\controller\product;

use app\common\model\SystemGoods;
use app\common\model\SystemGoodsDetail;
use library\Controller;
use think\Db;
use library\File;

/**
 * 产品管理
 * Class Product
 * @package app\admin\controller\product
 */
class Product extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'SystemProduct';

    /**
     * 产品列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '产品管理';
        $this->product_class_op = assemble_op([''=>'-- 全部产品分类 --'], get_all_product_class());
        $where = [];       
        $where[] = ['is_deleted','eq',0]; 
        $status = $this->request->get('status'); 
        if($status){
        	$where[] = ['status','eq',$status];    
        }
        
        $query = $this->_query($this->table)->like('code,name')->equal('product_class');
        $query->where($where)->dateBetween('create_at,update_at')->order('sort desc,id asc')->page();
    }
    
    
    /**
     * 列表数据处理
     * @auth true
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {  	
    	foreach($data as $vo){
    		$ids[] = $vo['id'];
    	}
		if(!empty($ids) && $ids){
			$product_class_op = get_all_product_class();
	        foreach ($data as &$vo) {       	
	        	$vo['class_name'] = $product_class_op[$vo['product_class']] ?? '';	      
	        }
		}
    }


    /**
     * 添加产品
     * @auth true
     */
    public function add()
    {
    	$this->product_class_op = get_normal_product_class();
        $this->_form($this->table, 'form');
    }

    public function upload()
    {
        $image = request()->file('image');
        
        $uptype = sysconf('storage_type');
        if(in_array($uptype,['oss', 'qiniu', 'cos'])){
        	$extend = pathinfo($image->getInfo('name'), PATHINFO_EXTENSION);       
	        $name = File::name($image->getPathname(), $extend, '', 'md5_file');     	
        	$name = explode('/',$name);
			$name = $name[1];
			$name = 'imgs/'.date('Ymd').'/'.$name;
			$info = File::instance($uptype)->save($name, file_get_contents($image->getRealPath()), false);
			if (is_array($info) && isset($info['url'])) {
	            $this->success('',$info['url']);
	       } else {
	            // 上传失败获取错误信息
	            $this->error('文件处理失败，请稍候再试！');
	        }
        }else{
        	$info = $image->move('upload');
	        if ($info) {
	            $this->success('',str_replace('\\','/','/upload/'.$info->getSaveName()));
	        } else {
	            // 上传失败获取错误信息
	            $this->error($image->getError());
	        }
        }
    }


    /**
     * 表单数据后置处理
     * @param number $id
     * @auth true
     */
    protected function _add_form_result($id)
    {
        $info = Db::name($this->table)->where(['id' => $id])->find();
        if ($info) {
            sysoplog('产品管理', '添加' . $info['name'] . '成功');
            $this->success('保存成功');
        }

    }

    /**
     * 编辑产品
     * @auth true
     */
    public function edit()
    {
    	$this->product_class_op = get_all_product_class();
        $this->_form($this->table, 'form');
    }


    /**
     * 表单数据后置处理
     * @param number $id
     * @auth true
     */
    protected function _edit_form_result($id)
    {
        $info = Db::name($this->table)->where(['id' => $id])->find();
        //修改订单的大分类
        Db::name('system_order')->where(['product_id'=>$id])->update(['product_class'=>$info['product_class']]);
        if ($info) {
            sysoplog('产品管理', '编辑' . $info['name'] . '成功');
            $this->success('保存成功');
        }
    }


    /**
     * 禁用产品
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '2']);
    }

    /**
     * 启用产品
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '1']);
    }
    

    /**
     * 自定义模版
     * @auth true
     *
     */
    public function productDetail()
    {   	
        $this->_form($this->table, 'product_detail');
    }

    
}
